/*******************************************************************************
 * Level Master 2000
 * Copyright (C) 2010 Christian Herzog
 * For more information see http://code.google.com/p/levelmaster2000/
 *
 * This file is licensed under the terms of the MIT license, see
 * http://www.opensource.org/licenses/mit-license.php for further information.
 ******************************************************************************/

#include "EntityExplorer.h"

#include "ui_EntityExplorer.h"
#include "ui_CreateNewEntityDialog.h"

#include "commands/CreateComponentCommand.h"
#include "commands/CreateGroupCommand.h"
#include "commands/CreateObjectCommand.h"
#include "commands/DeleteComponentCommand.h"
#include "commands/DeleteEntityCommand.h"
#include "commands/DeleteGroupCommand.h"
#include "commands/SelectEntitiesCommand.h"

#include "Config.h"
#include "LevelMaster2000.h"
#include "Logger.h"

#include <QPushButton>
#include <QLineEdit>
#include <QMessageBox>
#include <QInputDialog>
#include <QDialog>
#include <QCompleter>
#include <QStandardItemModel>
#include <QModelIndex>
#include <QUndoStack>

int EntityExplorer::entityCounter = 0;

EntityExplorer::EntityExplorer( QWidget *parent, QUndoStack *undoStack )
    : QDockWidget( parent ),
    undoStack( undoStack )
{
    init();
}

void EntityExplorer::init()
{
    Ui::entityExplorerWidget uiEntityExplorerWidget;
    uiEntityExplorerWidget.setupUi( this );

    componentCompleter = 0;
    componentCompleterTreeView = 0;

    //Connect the quick add component line edit:
    componentLineEdit = this->findChild<QLineEdit*>( "componentLineEdit" );

    componentLineEdit->connect( componentLineEdit,
                                SIGNAL( returnPressed() ),
                                this,
                                SLOT( createComponentFromLineEdit() ) );

    //Connect the "new entity" button:
    newEntityButton = this->findChild<QPushButton*>( "newEntityButton" );

    newEntityButton->connect( newEntityButton,
                              SIGNAL( clicked() ),
                              this,
                              SLOT( newEntityButtonClicked() ) );

    newGroupButton = this->findChild<QPushButton*>( "newGroupButton" );

    newGroupButton->connect( newGroupButton,
                             SIGNAL( clicked() ),
                             this,
                             SLOT( newGroupButtonClicked() ) );


    //Connect the "delete" button:
    QPushButton *deleteObjectButton = this->findChild<QPushButton*>( "deletePBObject" );

    deleteObjectButton->connect( deleteObjectButton,
                                 SIGNAL( clicked() ),
                                 this,
                                 SLOT( deleteObjectButtonClicked() ) );

    //Connect the "component browser" button:
    QPushButton *componentBrowserButton = this->findChild<QPushButton*>( "componentBrowserButton" );

    componentBrowserButton->connect( componentBrowserButton,
                                     SIGNAL( clicked() ),
                                     this,
                                     SLOT( showComponentBrowser() ) );

    //Connect entity explorer tree:
    tree = this->findChild<QTreeWidget*>( "entityExplorer" );

    tree->connect( tree,
                   SIGNAL( itemClicked( QTreeWidgetItem*, int ) ),
                   this,
                   SLOT( clicked( QTreeWidgetItem*, int ) ) );

    tree->connect( tree,
                   SIGNAL( itemDoubleClicked( QTreeWidgetItem*, int ) ),
                   this,
                   SLOT( doubleClicked( QTreeWidgetItem*, int ) ) );

    tree->connect( tree,
                   SIGNAL( itemSelectionChanged() ),
                   this,
                   SLOT( itemSelectionChanged() ) );

    //Connect the delete component button:
    QPushButton *deleteComponentButton = this->findChild<QPushButton*>( "deleteComponentButton" );

    deleteComponentButton->connect( deleteComponentButton,
                                     SIGNAL( clicked() ),
                                     this,
                                     SLOT( deleteComponentClicked() ) );


    PBObjectNameLineEdit = this->findChild<QLineEdit*>( "PBObjectName" );

    tree->setSelectionMode( QAbstractItemView::ExtendedSelection );

    componentList = this->findChild<QTableWidget*>( "componentList" );
    componentList->setSelectionBehavior( QAbstractItemView::SelectRows	);

    componentListLabel = this->findChild<QLabel*>( "componentListLabel" );
}

void EntityExplorer::initComponentCompleter( const QList<ClassInfo> &componentInfo )
{
    delete componentCompleter;

    componentCompleter = new QCompleter( this );

    QStandardItemModel *model = new QStandardItemModel( componentInfo.count(),
                                                        2,
                                                        componentCompleter );

    for( int i = 0; i < componentInfo.count(); ++i )
    {
        QModelIndex index = model->index( i, 0 );
        model->setData( index, componentInfo[i].name );

        QModelIndex packageIndex = model->index( i, 1 );
        QString packageName;

        int j = 0;
        foreach( QString packageBit, componentInfo[i].package )
        {
            if( j != 0 )
            {
                packageName.append( "." );
            }
            packageName.append( packageBit );
            j++;
        }
        model->setData( packageIndex, packageName );
    }

    //In case tree view is wanted:
    delete componentCompleterTreeView;
    componentCompleterTreeView = new QTreeView();
    componentCompleter->setPopup( componentCompleterTreeView );
    componentCompleterTreeView->setRootIsDecorated( false );
    componentCompleterTreeView->header()->hide();
    componentCompleterTreeView->header()->setStretchLastSection( true );

    componentCompleter->setModel( model );
    componentCompleter->setCaseSensitivity( Qt::CaseInsensitive );
    componentLineEdit->setCompleter( componentCompleter );
}

void EntityExplorer::itemSelectionChanged()
{
    QList<QTreeWidgetItem*> items = tree->selectedItems();

    Entity *ent = 0;

    if( items.size() == 1 )
    {
        ent = findEntity( items[0] );

        if( ent )
        {
            enableCreation( false );
            componentListLabel->setText( "Components of " + ent->name + ":" );
        }
        else
        {
            enableCreation( true );
            componentListLabel->setText( "Group selected, no components." );
        }
    }
    else if( items.size() > 1 )
    {
        enableCreation( false );
        componentListLabel->setText( "Multiple selection not supported." );
    }
    else
    {
        enableCreation( false );
        componentListLabel->setText( "No Entity selected." );
    }

    listComponents( ent );
}

void EntityExplorer::deleteComponentClicked()
{
    QList<QTableWidgetItem*> items = componentList->selectedItems();

    QString entityName = selectedEntities()[0]->name;

    foreach( QTableWidgetItem* item, items )
    {
        if( item->column() == Config::ENTITY_EXPLORER_COMPONENT_NAME_COLUMN )
        {
            QString componentName = item->text();

            deleteComponent( entityName, componentName );
            //emit sigDeleteComponent( entityName, componentName );
        }
    }

    listComponents( findEntity( entityName ) );
}

QList<Entity*> EntityExplorer::selectedEntities()
{
    QList<Entity*> entities;

    foreach( QTreeWidgetItem* item, tree->selectedItems() )
    {
        EntityTreeWidgetItem *entityItem = dynamic_cast<EntityTreeWidgetItem*>( item );
        if( entityItem )
        {
            entities.push_back( entityItem->entity );
        }
    }

    return entities;
}

void EntityExplorer::addEntities( GroupTreeWidgetItem *groupItem, QList<Entity*> &entities )
{
    for( int i = 0; i < groupItem->childCount(); i++ )
    {
        QTreeWidgetItem *item = groupItem->child( i );
        GroupTreeWidgetItem* groupItem = dynamic_cast<GroupTreeWidgetItem*>( item );
        EntityTreeWidgetItem* entityItem = dynamic_cast<EntityTreeWidgetItem*>( item );

        if( groupItem )
        {
            addEntities( groupItem, entities );
        }
        else if( entityItem )
        {
            entities.push_back( entityItem->entity );
        }
    }
}

void EntityExplorer::onRequestSelectEntity( QString entityName )
{
    EntityTreeWidgetItem *item = findTreeItemByName<EntityTreeWidgetItem*>( entityName );

    QList<Entity*> entities;

    if( item )
    {
        entities.push_back( item->entity );
    }

    onSelectEntities( entities );
}

//TODO: cant this be done with tree->selectedItems?!
void EntityExplorer::onSelectEntities( QList<Entity*> newEntities )
{
    //Logger::instance()->addLogMessage( Logger::LL_ERROR, "test", "onSelectEntities" );
    undoStack->push( new SelectEntitiesCommand( this,
                                                newEntities,
                                                lastSelection ) );

    lastSelection = newEntities;
}

Entity* EntityExplorer::findEntity( QString name )
{
    EntityTreeWidgetItem *entityItem = findTreeItemByName<EntityTreeWidgetItem*>( name );

    if( entityItem )
    {
        return entityItem->entity;
    }
    else
    {
        Logger::instance()->addLogMessage( Logger::LL_WARNING,
                               "EntityExplorer::findEntity",
                               "Couldn't find entity " + name );
    }

    return 0;
}

void EntityExplorer::addEntityToSelection( Entity* entity )
{
    if( !entity )
    {
        Logger::instance()->addLogMessage( Logger::LL_WARNING,
                               "EntityExplorer::addEntityToSelection",
                               "Entity is null." );

        return;
    }

    QList<Entity*> entities;
    entities << entity;

    addEntitiesToSelection( entities );
}

void EntityExplorer::addEntitiesToSelection( QList<Entity*> entities )
{
    QList<Entity *> newEntitySelection = selectedEntities();

    foreach( Entity *entity, entities )
    {
        QTreeWidgetItem *entityItem = findTreeItemByName<QTreeWidgetItem*>( entity->name );
        entityItem->setSelected( true );
        newEntitySelection.push_back( entity );
    }

    //Communicator catches this signal...
    emit selectEntities( newEntitySelection );
    //emit selectEntities( entities );
}

//TODO: double check this again!
void EntityExplorer::removeEntityFromSelection( Entity* entity )
{
    QList<Entity*> entities = selectedEntities();

    bool found = false;
    foreach( Entity *iterEntity, entities )
    {
        if( entity == iterEntity )
        {
            found = true;
            break;
        }
    }

    if( found )
    {
        EntityTreeWidgetItem* item = findTreeItemByName<EntityTreeWidgetItem*>( entity->name );
        item->setSelected( false );
    }

    entities = selectedEntities();
    emit selectEntities( entities );
}

void EntityExplorer::updateLastSelection()
{
    lastSelection = selectedEntities();
    int i = 0;
    i++;
}

void EntityExplorer::enableCreation( bool enable )
{
    newEntityButton->setEnabled( enable );
    newGroupButton->setEnabled( enable );
}

void EntityExplorer::clicked( QTreeWidgetItem *item, int column )
{
    if( dynamic_cast<GroupTreeWidgetItem*>( item ) )
    {
        return;
    }

    QTreeWidgetItem *selectedItem = 0;
    QList<QTreeWidgetItem*> items = tree->selectedItems();
    QList<Entity*> newEntities;

    foreach( selectedItem, items )
    {
        Entity *ent = findEntity( selectedItem );

        if( ent )
        {
            newEntities.push_back( ent );
        }
    }

    onSelectEntities( newEntities );
}

void EntityExplorer::doubleClicked( QTreeWidgetItem *item, int column )
{
    QTreeWidgetItem *selectedItem = 0;
    QList<QTreeWidgetItem*> items = tree->selectedItems();

    QList<Entity*> newEntities;

    foreach( selectedItem, items )
    {
        GroupTreeWidgetItem* groupItem = dynamic_cast<GroupTreeWidgetItem*>( selectedItem );
        if( groupItem )
        {
            addEntities( groupItem, newEntities );
        }
    }

    //Why 2 times foreach?
    foreach( selectedItem, items )
    {
        Entity *ent = findEntity( selectedItem );

        if( ent )
        {
            newEntities.push_back( ent );
        }
    }

    onSelectEntities( newEntities );
}


void EntityExplorer::listComponents( Entity *entity )
{
    //Select no row:
    componentList->selectRow( 0 );
    componentList->selectionModel()->clearSelection();

    if( entity == 0 )
    {
        componentList->clearContents();
        return;
    }

    int numComponents = entity->components.size();

    componentList->setRowCount( numComponents );

    for( int i = 0; i < numComponents; i++ )
    {
        Component* comp = entity->components[i];
        QTableWidgetItem *nameItem = new QTableWidgetItem();
        nameItem->setText( comp->name );
        componentList->setItem( i,
                                Config::ENTITY_EXPLORER_COMPONENT_NAME_COLUMN,
                                nameItem );

        QTableWidgetItem *classItem = new QTableWidgetItem();
        classItem->setText( comp->classInfo.name );
        classItem->setToolTip( comp->classInfo.buildFullClassName() );
        componentList->setItem( i, 1, classItem );
    }
}

Entity* EntityExplorer::findEntity(QTreeWidgetItem *item)
{
    if( dynamic_cast<ComponentTreeWidgetItem*>(item) )
    {
        return dynamic_cast<ComponentTreeWidgetItem*>(item)->owner;
    }

    else if( dynamic_cast<EntityTreeWidgetItem*>(item) )
    {
        return dynamic_cast<EntityTreeWidgetItem*>(item)->entity;
    }

    else
    {
        return 0;
    }
}

void EntityExplorer::createComponentFromLineEdit()
{
    QString fullName;

    QModelIndexList selected = componentCompleterTreeView->selectionModel()->selectedIndexes();

    if( selected.count() == 2 )
    {
        fullName = selected[1].data().toString()
                   + "."
                   + selected[0].data().toString();
    }
    else
    {
        QMessageBox::warning( this,
                              "Quick Create Component",
                              "Creation failed: no selection.\nUse auto complete popup." );
    }

    componentLineEdit->setText( fullName );
}

void EntityExplorer::onNewComponent( QString entityName, QString componentName, QString componentClass )
{
     EntityTreeWidgetItem *entityItem = findTreeItemByName<EntityTreeWidgetItem*>( entityName );

    if( entityItem != 0 )
    {
        ClassInfo ci ( componentClass );
        entityItem->entity->components.push_back( new Component( componentName, ci ) );

        if( !LevelMaster2000::DISALLOW_NEW_COMMAND )
        {
            LevelMaster2000::DISALLOW_NEW_COMMAND++;
            undoStack->push( new CreateComponentCommand( this,
                                                entityName,
                                                componentName,
                                                componentClass ) );
           LevelMaster2000::DISALLOW_NEW_COMMAND--;
        }
    }
}

void EntityExplorer::createComponent( QString componentName, QString className )
{
    foreach( Entity *entity, selectedEntities() )
    {
        emit requestCreateComponent( entity->name, componentName, className );
    }
}

void EntityExplorer::newGroupButtonClicked()
{
    //Get the currently selected group.
    QTreeWidgetItem *item = tree->currentItem();
    GroupTreeWidgetItem *groupItem = dynamic_cast<GroupTreeWidgetItem*>( item );

    if( groupItem )
    {
        //PBE will return a valid entity name, so just send it.
        //emit requestCreateGroup( PBObjectNameLineEdit->text(), groupItem->group->name );
        createGroup( PBObjectNameLineEdit->text(), groupItem->group->name );
    }
}

void EntityExplorer::newEntityButtonClicked()
{
    //Get the currently selected group.
    QTreeWidgetItem *item = tree->currentItem();
    GroupTreeWidgetItem *groupItem = dynamic_cast<GroupTreeWidgetItem*>( item );

    if( groupItem )
    {
        //emit requestCreateEntity( PBObjectNameLineEdit->text(), groupItem->group->name );
        createEntity( PBObjectNameLineEdit->text(), groupItem->group->name );
    }
}

void EntityExplorer::createEntity( QString entity, QString group )
{
    //PBE will return a valid entity name, so just send it.
    emit requestCreateEntity( entity, group );

//    LevelMaster2000::DISALLOW_NEW_COMMAND++;
//    undoStack->push( new CreateObjectCommand( this,
//                                              entity,
//                                              group ) );
//    LevelMaster2000::DISALLOW_NEW_COMMAND--;
}

void EntityExplorer::createGroup( QString group, QString parentGroup )
{
    //PBE will return a valid entity name, so just send it.
    emit requestCreateGroup( group, parentGroup );
}


//TODO: maybe implement batching?
void EntityExplorer::deleteObjectButtonClicked()
{
    QList<QTreeWidgetItem*> items = tree->selectedItems();

    QStringList objects;
    foreach( QTreeWidgetItem *item, items )
    {
        GroupTreeWidgetItem* groupItem = dynamic_cast<GroupTreeWidgetItem*>( item );
        EntityTreeWidgetItem* entityItem = dynamic_cast<EntityTreeWidgetItem*>( item );

        if( groupItem )
        {
            //objects.push_back( groupItem->group->name  );
            if( groupItem->childCount() == 0 )
            {
                LevelMaster2000::DISALLOW_NEW_COMMAND++;
                undoStack->push( new DeleteGroupCommand( this,
                                                          groupItem->group->name,
                                                          groupItem->group->parent->name ) );
                LevelMaster2000::DISALLOW_NEW_COMMAND--;
            }
            else
            {
                QMessageBox::warning( this,
                                      "Not supported yet.",
                                      "Currently only deletion of empty groups supported." );
            }
        }
        else if( entityItem )
        {
            //deleteEntity( entityItem->entity->name );
            //objects.push_back( entityItem->entity->name  );
            if( !LevelMaster2000::DISALLOW_NEW_COMMAND )
            {
                getEntityXml( entityItem->entity->name );

                LevelMaster2000::DISALLOW_NEW_COMMAND++;
                undoStack->push( new DeleteEntityCommand( this,
                                                          entityItem->entity->name,
                                                          entityItem->entity->group->name,
                                                          entityXml ) );
                LevelMaster2000::DISALLOW_NEW_COMMAND--;
            }
        }
    }

    //emit deleteObjects( objects );
}

void EntityExplorer::deleteComponent( QString entityName, QString componentName )
{
//    Entity* entity = findEntity( entityName );
//
//    if( !entity )
//    {
//        Logger::instance()->addLogMessage( Logger::LL_WARNING,
//                                           "EntityExplorer::deleteComponent",
//                                           "Entity not found: " + entityName );
//        return;
//    }

    if( !LevelMaster2000::DISALLOW_NEW_COMMAND )
    {
        //NOTE: Currently I assume (and it appears to be so) that all the getEntityXml code is executed
        //before going on to creating the DeleteComponentCommand.
        //TODO: one should further investigate, if there are situations, where this might not be the case.
        getComponentXml( entityName, componentName );

//        LevelMaster2000::DISALLOW_NEW_COMMAND++;
//        undoStack->push( new DeleteComponentCommand( this,
//                                                  entityName,
//                                                  componentName,
//                                                  componentXml ) );
//        LevelMaster2000::DISALLOW_NEW_COMMAND--;
    }

    emit sigDeleteComponent( entityName, componentName );
}

void EntityExplorer::deleteEntity( QString name )
{
    QStringList entities;
    EntityTreeWidgetItem* entityItem = findTreeItemByName<EntityTreeWidgetItem*>( name );

    if( entityItem )
    {
        entities.push_back( entityItem->entity->name  );
    }

    if( entities.size() == 1 )
    {
        //NOTE: Currently I assume (and it appears to be so) that all the getEntityXml code is executed
        //before going on to creating the DeleteEntityCommand.
        //TODO: one should further investigate, if there are situations, where this might not be the case.
        //getEntityXml( entityItem->entity->name );

        if( !LevelMaster2000::DISALLOW_NEW_COMMAND )
        {
            getEntityXml( entityItem->entity->name );

            LevelMaster2000::DISALLOW_NEW_COMMAND++;
            undoStack->push( new DeleteEntityCommand( this,
                                                      entityItem->entity->name,
                                                      entityItem->entity->group->name,
                                                      entityXml ) );
            LevelMaster2000::DISALLOW_NEW_COMMAND--;
        }

        emit deleteObjects( entities );
    }
    else
    {
        Logger::instance()->addLogMessage( Logger::LL_WARNING,
                               "EntityExplorer::deleteEntity",
                               "Couldn't find entity " + name + "." );
    }
}

void EntityExplorer::deleteGroup( QString name )
{
    QStringList groups;
    GroupTreeWidgetItem* groupItem = findTreeItemByName<GroupTreeWidgetItem*>( name );

    if( groupItem )
    {
        groups.push_back( groupItem->group->name  );
    }

    if( groups.size() > 0 )
    {
        emit deleteObjects( groups );
    }
    else
    {
        Logger::instance()->addLogMessage( Logger::LL_WARNING,
                               "EntityExplorer::deleteGroup",
                               "Couldn't find group " + name + "." );
    }
}

void EntityExplorer::getEntityXml( QString name )
{
    entityXml = "";
    emit requestGetEntityXml( name );
}

void EntityExplorer::setEntityXml( QString name, QString xml )
{
    emit requestSetEntityXml( name, xml );
}

void EntityExplorer::getComponentXml( QString entityName, QString componentName )
{
    componentXml = "";
    emit requestGetComponentXml( entityName, componentName );
}

void EntityExplorer::setComponentXml( QString entityName, QString componentName, QString xml )
{
    emit requestSetComponentXml( entityName, componentName, xml );
}

void EntityExplorer::showComponentBrowser()
{
    emit showComponentBrowser( true );
}

void EntityExplorer::onNewEntity( QString name, QString group )
{
    GroupTreeWidgetItem *groupItem = findTreeItemByName<GroupTreeWidgetItem*>( group );

    Entity *entity = new Entity( name );
    entity->group = groupItem->group;

    EntityTreeWidgetItem *entityItem = new EntityTreeWidgetItem( groupItem, entity );
    entityItem->setText( 0, name );
    QIcon entityIcon ("../../Cpp/ui/icons/entity.png");
    entityItem->setIcon( 0, entityIcon );

    tree->sortItems( 0, Qt::AscendingOrder );

    if( !LevelMaster2000::DISALLOW_NEW_COMMAND )
    {
        LevelMaster2000::DISALLOW_NEW_COMMAND++;
        undoStack->push( new CreateObjectCommand( this,
                                            entity->name,
                                            entity->group->name ) );
       LevelMaster2000::DISALLOW_NEW_COMMAND--;
    }
}

void EntityExplorer::onNewGroup( QString name, QString parent )
{
    GroupTreeWidgetItem *parentGroupItem = findTreeItemByName<GroupTreeWidgetItem*>( parent );

    Group *group = new Group( name, parentGroupItem->group );

    GroupTreeWidgetItem *groupItem = new GroupTreeWidgetItem( parentGroupItem, group );
    groupItem->setText( 0, name );
    QIcon groupIcon ("../../Cpp/ui/icons/group.png");
    groupItem->setIcon( 0, groupIcon );

    tree->sortItems( 0, Qt::AscendingOrder );

    if( !LevelMaster2000::DISALLOW_NEW_COMMAND )
    {
        LevelMaster2000::DISALLOW_NEW_COMMAND++;
        undoStack->push( new CreateGroupCommand( this,
                                            group->name,
                                            group->parent->name ) );
       LevelMaster2000::DISALLOW_NEW_COMMAND--;
    }

}

void EntityExplorer::onDeleteObjects( QStringList objects )
{
    foreach( QString name, objects )
    {
        QList<QTreeWidgetItem*> items = tree->findItems( name, Qt::MatchRecursive, 0 );

        if( items.count() == 1 )
        {
            delete items[0];
        }
    }
}

void EntityExplorer::onDeleteComponent( QString entityName, QString componentName )
{
    //Deletes the component from the entity.

    Entity *entity = findEntity( entityName );

    if( !entity )
    {
        Logger::instance()->addLogMessage( Logger::LL_WARNING,
                               "EntityExplorer::onDeleteComponent",
                               "Couldn't find entity " + entityName );
        return;
    }

    int numComponents = entity->components.size();

    int idx = -1;
    for( int i = 0; i < numComponents; i++ )
    {
        if( entity->components[i]->name == componentName )
        {
            idx = i;
            break;
        }
    }

    if( idx == -1 )
    {
        Logger::instance()->addLogMessage( Logger::LL_WARNING,
                               "EntityExplorer::onDeleteComponent",
                               "Couldn't find component " + componentName );
        return;
    }

    entity->components.removeAt( idx );

    //Called in deleteComponentClicked...
    //listComponents( entity );
}

//TODO: this is currently assuming that it's only called if one wants to delete a component.
//That sucks bad ass... but time is flying :/
void EntityExplorer::onGetComponentXml( QString entityName, QString componentName, QString className, QString xml )
{
    componentXml = xml;

    if( !LevelMaster2000::DISALLOW_NEW_COMMAND )
    {
        LevelMaster2000::DISALLOW_NEW_COMMAND++;
        undoStack->push( new DeleteComponentCommand( this,
                                                  entityName,
                                                  componentName,
                                                  className,
                                                  componentXml ) );
        LevelMaster2000::DISALLOW_NEW_COMMAND--;
    }

    emit sigDeleteComponent( entityName, componentName );
}

void EntityExplorer::onGetEntityXml( QString name, QString xml )
{
    entityXml = xml;

//    QStringList entities;
//    EntityTreeWidgetItem* entityItem = findTreeItemByName<EntityTreeWidgetItem*>( name );
//
//    if( entityItem )
//    {
//        entities.push_back( entityItem->entity->name  );
//    }
//
//    if( entities.size() == 1 )
//    {
//        LevelMaster2000::DISALLOW_NEW_COMMAND++;
//        undoStack->push( new DeleteEntityCommand( this,
//                                                  entityItem->entity->name,
//                                                  entityXml ) );
//        LevelMaster2000::DISALLOW_NEW_COMMAND--;
//
//        emit deleteObjects( entities );
//    }
}

void EntityExplorer::onSetEntityXml( QString name, QString xml )
{

}

